﻿using System;
using System.Runtime.Serialization;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using Game.Common;
using Game.Common.Resources;
using Game.ExtensibleObjects;
using Game.Map;
using Game.Terrain;
using Game.Types;

namespace Game.Objects
{
    /// <summary>
    /// The parent class of Vehicle and Building;.
    /// </summary>
    [DataContract(Namespace="game")]
    public abstract class ContainerBase : IRuntimeExtensibleObject, IHasOwner
    {
        public event EventHandler<HasOwnerEventArgs> PlayerChanged;
        public event EventHandler<ContainerEventArgs> Changed;
        
        readonly ContainerBaseType _ContainerBaseType;

        int _Damage;
        
        internal ContainerBase()
        {
        }

        protected ContainerBase(ContainerBaseType containerBaseType)
        {
            _ContainerBaseType = containerBaseType;
            Extensions = containerBaseType.RuntimeExtensionFactories.GetExtensionCollection(this);
            Movement.Changed += (source, args) =>
                                        {
                                            FireChanged();
                                        };
        }
        
        public IMapItemType MapItemType 
        {
            get { return _ContainerBaseType; }
        }
        
        public int Damage
        {
            get { return _Damage; }
            set { _Damage=value; FireChanged(); }
        }

        public double TerrainCost 
        {
            get { return 0; } 
        }

        public IGlobalItem Building
        {
            get { return null; }
        }

        public RuntimeExtensionCollection Extensions { get; protected set; }

        public abstract GameMovement Movement { get; }
        
        public abstract GameResources GameResources { get; }
        
        public abstract GamePlayer Owner { get; set; }
        
        public abstract ReadOnlyTerrainAccess TerrainAccess { get; }

        public abstract ImageSource Picture { get; }
        
        public void Init(IGameMap map)
        {
        }
        
        protected void DoPlayerChanged()
        {
            if (PlayerChanged != null)
            {
                var args = new HasOwnerEventArgs(this);
                PlayerChanged(this, args);
            }
        }
        
        protected void FireChanged()
        {
            if (Changed != null)
            {
                var args = new ContainerEventArgs(this);
                Changed(this, args);
            }
        }
    }

    public class ContainerEventArgs : EventArgs
    {
        readonly ContainerBase _ContainerBase;

        public ContainerEventArgs(ContainerBase containerBase)
        {
            _ContainerBase = containerBase;
        }

        public ContainerBase ContainerBase
        {
            get { return _ContainerBase; }
        }
    }
    
}
